// 认证相关hooks

import { useState, useCallback } from 'react';
import {
  login as apiLogin,
  register as apiRegister,
  registerAdmin as apiRegisterAdmin,
  sendVerificationCode as apiSendVerificationCode,
  resetPassword as apiResetPassword,
  changePassword as apiChangePassword,
  refreshToken as apiRefreshToken,

} from '../api';
import {
  LoginRequest,
  RegisterRequest,
  RegisterAdminRequest,
  SendVerificationCodeRequest,
  ResetPasswordRequest,
  ChangePasswordRequest,
  User,
} from '../types';
import { handleApiError, tokenStorage, userStorage } from '../utils/api';

export const useAuth = () => {
  const [loading, setLoading] = useState(false);
  const [error, setError] = useState<string | null>(null);

  // 登录
  const login = useCallback(async (data: LoginRequest) => {
    setLoading(true);
    setError(null);
    
    try {
      const response = await apiLogin(data);
      if (response.code === 200) {
        tokenStorage.set(response.data.token);
        userStorage.set(response.data.user);
        return response.data;
      } else {
        throw new Error(response.message);
      }
    } catch (err) {
      const errorMessage = handleApiError(err);
      setError(errorMessage);
      throw new Error(errorMessage);
    } finally {
      setLoading(false);
    }
  }, []);

  // 注册
  const register = useCallback(async (data: RegisterRequest) => {
    setLoading(true);
    setError(null);
    
    try {
      const response = await apiRegister(data);
      if (response.code === 200) {
        return response.data;
      } else {
        throw new Error(response.message);
      }
    } catch (err) {
      const errorMessage = handleApiError(err);
      setError(errorMessage);
      throw new Error(errorMessage);
    } finally {
      setLoading(false);
    }
  }, []);

  // 管理员注册
  const registerAdmin = useCallback(async (data: RegisterAdminRequest) => {
    setLoading(true);
    setError(null);
    
    try {
      const response = await apiRegisterAdmin(data);
      if (response.code === 200) {
        return response.data;
      } else {
        throw new Error(response.message);
      }
    } catch (err) {
      const errorMessage = handleApiError(err);
      setError(errorMessage);
      throw new Error(errorMessage);
    } finally {
      setLoading(false);
    }
  }, []);

  // 发送验证码
  const sendVerificationCode = useCallback(async (data: SendVerificationCodeRequest) => {
    setLoading(true);
    setError(null);
    
    try {
      const response = await apiSendVerificationCode(data);
      if (response.code === 200) {
        return true;
      } else {
        throw new Error(response.message);
      }
    } catch (err) {
      const errorMessage = handleApiError(err);
      setError(errorMessage);
      throw new Error(errorMessage);
    } finally {
      setLoading(false);
    }
  }, []);

  // 重置密码
  const resetPassword = useCallback(async (data: ResetPasswordRequest) => {
    setLoading(true);
    setError(null);
    
    try {
      const response = await apiResetPassword(data);
      if (response.code === 200) {
        return true;
      } else {
        throw new Error(response.message);
      }
    } catch (err) {
      const errorMessage = handleApiError(err);
      setError(errorMessage);
      throw new Error(errorMessage);
    } finally {
      setLoading(false);
    }
  }, []);

  // 修改密码
  const changePassword = useCallback(async (data: ChangePasswordRequest) => {
    setLoading(true);
    setError(null);
    
    try {
      const response = await apiChangePassword(data);
      if (response.code === 200) {
        return true;
      } else {
        throw new Error(response.message);
      }
    } catch (err) {
      const errorMessage = handleApiError(err);
      setError(errorMessage);
      throw new Error(errorMessage);
    } finally {
      setLoading(false);
    }
  }, []);

  // 刷新Token
  const refreshToken = useCallback(async () => {
    setLoading(true);
    setError(null);
    
    try {
      const response = await apiRefreshToken();
      if (response.code === 200) {
        tokenStorage.set(response.data.token);
        userStorage.set(response.data.user);
        return response.data;
      } else {
        throw new Error(response.message);
      }
    } catch (err) {
      const errorMessage = handleApiError(err);
      setError(errorMessage);
      throw new Error(errorMessage);
    } finally {
      setLoading(false);
    }
  }, []);

  return {
    loading,
    error,
    login,
    register,
    registerAdmin,
    sendVerificationCode,
    resetPassword,
    changePassword,
    refreshToken,
  };
}; 